<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Web高级开发 | Advanced Web Development Course</title>
    <link
      href="https://fonts.googleapis.com/css?family=Heebo:300,400"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="../../css/main.css" />
    <link rel="stylesheet" href="../../css/tabs.css" />
    <script src="../../js/uikit.js"></script>
    <link href="../../css/prism.css" rel="stylesheet" />
    <script src="../../js/prism.js"></script>
  </head>

  <body>
    <div
      data-uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent; top: 300"
    >
      <nav class="uk-navbar-container pt-2 pb-2">
        <div class="uk-container">
          <div data-uk-navbar>
            <div class="uk-navbar-left">
              <a
                class="uk-navbar-item uk-logo uk-visible@m"
                href="../../index.html"
                >Web高级开发课程</a
              >
              <a
                class="uk-navbar-tuk-containeroggle uk-hidden@m"
                href="#offcanvas-docs"
                data-uk-toggle
                ><span data-uk-navbar-toggle-icon></span>
                <span class="uk-margin-small-left">目录</span></a
              >
              <ul class="uk-navbar-nav uk-visible@m">
                <li><a href="../../index.html">文档</a></li>
              </ul>
            </div>
            <div class="uk-navbar-center uk-hidden@m">
              <a class="uk-navbar-item uk-logo" href="../../index.html"
                >Web高级开发课程</a
              >
            </div>
          </div>
        </div>
      </nav>
    </div>

    <div class="uk-section">
      <div class="uk-container">
        <div class="uk-grid-large" data-uk-grid>
          <div class="sidebar-fixed-width uk-visible@m">
            <div class="sidebar-docs uk-position-fixed uk-margin-top">
              <h5>关于这门课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../../index.html">大纲</a></li>
              </ul>
              <h5>章节</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="../01.html">第1章 - Spring基础</a></li>
                <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
                <li><a href="../03.html">第3章 - Spring AOP</a></li>
                <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
                <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
                <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
                <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
                <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
                <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
                <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
                <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
                <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
                <li>
                  <a href="../13.html">第13章 - Spring Boot的数据库连接</a>
                </li>
                <li><a href="../14.html">第14章 - 框架整合</a></li>
              </ul>
              <h5>实验课</h5>
              <ul class="uk-nav uk-nav-default doc-nav">
                <li><a href="./code-01.html">01</a></li>
                <li><a href="./code-02.html">02</a></li>
                <li><a href="./code-03.html">03</a></li>
                <li><a href="./code-04.html">04</a></li>
                <li><a href="./code-05.html">05</a></li>
                <li><a href="./code-06.html">06</a></li>
                <li><a href="./code-07.html">07</a></li>
                <li><a href="./code-08.html">08</a></li>
                <li class="uk-active"><a href="./code-09.html">09</a></li>
                <li><a href="./code-10.html">10</a></li>
                <li><a href="./code-11.html">11</a></li>
                <li><a href="./code-12.html">12</a></li>
              </ul>
            </div>
          </div>
          <div class="uk-width-1-1 uk-width-expand@m">
            <article class="uk-article">
              <h1>实验课09 - Spring Boot快速上手</h1>
              <div>
                    <h2>1. 创建新项目 Hello World</h2>
                <p>File --&gt; New --&gt; Project --&gt; Spring Starter Project  </p>
                <p>点击 Next</p>
                <p>给项目起个名字(在Name选项里)</p>
                <p>点击 Next</p>
                <p>在Web选项里选择Spring Web --&gt; Finish</p>
                <h2>2. 编辑项目</h2>
                <p>HelloWorldDemoApplication.java</p>
<pre><code class='language-java' lang='java'>@SpringBootApplication
public class HelloWorldDemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(HelloWorldDemoApplication.class, args);
  }
}
</code></pre>
<p>controllers/HelloWorldController.java</p>
<pre><code class='language-java' lang='java'>@Controller
public class HelloWorldController {
  @ResponseBody
  @RequestMapping(&quot;/hello&quot;)
  public String hello() {
    return &quot;Hello World&quot;;
  }
}
</code></pre>
                <p>回到HelloWorldDemoApplication.java --&gt; 右键 --&gt; Run As --&gt; Java Application</p>
                <h2>3. 稍微深入了解一下</h2>
                <p>@SpringBootApplication 的内部有一个 @SpringBootConfiguration 的注解，它本质上就是一个@Configuration, 也就是一个配置文件。而它的本质又是一个@Component（组件）。</p>
                <p>&nbsp;</p>
                <p>@SpringBootApplication 内部还有一个@EnableAutoConfiguration，它的内部是 @AutoConfigurationPackage，它有Import了AutoConfigurationPackages.Registrar，而这个Registar的作用，是自动地把当前包及其子包下的组件，扫描到Spring容器里面。</p>
                <h2>4. 模板引擎</h2>
                <p>thymeleaf:</p>
                <p>在pom.xml文件中，加入依赖thymeleaf dependency:</p>
<pre><code class='language-xml' lang='xml'>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
                <p>然后，右键项目 --&gt; Maven --&gt; Update Project 来更新项目的依赖包，从而可以使用thymeleaf这个模板引擎。</p>
                <p>在Controller中，写一个success方法</p>
<pre><code class='language-java' lang='java'>@RequestMapping(&quot;/success&quot;)
    public String success(Map&lt;String, Object&gt; map) {
    map.put(&quot;hello&quot;,&quot;你好12312&quot;);
    return &quot;success.html&quot;;
}</code></pre>
                <p>它的作用就是把hello这个变量中的值“你好12312”发送给前端的html文件里。其中success.html是写在resources/templates中的。</p>
                <h2>5. Spring Boot热更新</h2>
                <p>在pom.xml文件中，加入</p>
<pre><code class='language-xml' lang='xml'>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
    &lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
</code></pre>
                <p>和</p>
<pre><code class='language-xml' lang='xml'>&lt;plugin&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
      &lt;fork&gt;true&lt;/fork&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;
</code></pre>
                <p>然后更新一下Maven项目，让依赖导入咱们的项目。</p>
                <h2>6. 名媛项目</h2>
                <p>controller class</p>
<pre><code class='language-java' lang='java'>@Controller
public class MingyuanController {
  
    @RequestMapping(&quot;/mingyuan&quot;)
    public String success(Map&lt;String, Object&gt; map) {
      int price = 8;
      int numberOfPeople = 4;
      int unitPrice = price / numberOfPeople;
      String[] users = {&quot;zhang3&quot;, &quot;li4&quot;, &quot;wang5&quot;, &quot;sheme6&quot;};
      map.put(&quot;price&quot;, price);
      map.put(&quot;numberOfPeople&quot;, numberOfPeople);
      map.put(&quot;unitPrice&quot;, unitPrice);
      map.put(&quot;userList&quot;, users);
      return &quot;mingyuan&quot;;
    }
}
</code></pre>
                <p>前端文件html</p>
                <p>这个html文件，应放在resources/templates里面</p>
                <p>命名空间 xmlns:th=&quot;<a href='http://www.thymeleaf.org' target='_blank' class='url'>http://www.thymeleaf.org</a>&quot;</p>
<pre><code class='language-html' lang='html'>&lt;!DOCTYPE html&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot; lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;烤冷面名媛拼单网&lt;/h1&gt;
    &lt;img src=&quot;https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=435541810,2243191753&amp;fm=26&amp;gp=0.jpg&quot; alt=&quot;food&quot;/&gt;
  &lt;p&gt;你们负责拍，我负责吃。与冷面合拍，尽显奢华。&lt;/p&gt;
  &lt;p&gt;单价：&lt;span th:text=&quot;${price}&quot;&gt;&lt;/span&gt;元 &lt;/p&gt;
  &lt;p&gt;拼单人数：&lt;span th:text=&quot;${numberOfPeople}&quot;&gt;&lt;/span&gt;人&lt;/p&gt;
  &lt;p&gt;平均每人仅需：&lt;span th:text=&quot;${unitPrice}&quot;&gt;&lt;/span&gt;元&lt;/p&gt;
  &lt;h3&gt;拼单名媛名单&lt;/h3&gt;
  &lt;div th:each=&quot;user : ${userList}&quot;&gt;
    &lt;div th:text=&quot;${user}&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>


              </div>
            </article>
          </div>
        </div>
      </div>
    </div>




    <div id="offcanvas-docs" data-uk-offcanvas="overlay: true">
      <div class="uk-offcanvas-bar">
        <button class="uk-offcanvas-close" type="button" data-uk-close></button>
        <h5 class="uk-margin-top">关于这门课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../indext.html">大纲</a></li>
        </ul>
        <h5 class="uk-margin-top">章节</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="../01.html">第1章 - Spring基础</a></li>
          <li><a href="../02.html">第2章 - Spring中的Bean</a></li>
          <li><a href="../03.html">第3章 - Spring AOP</a></li>
          <li><a href="../04.html">第4章 - Spring的事物管理</a></li>
          <li><a href="../05.html">第5章 - 初识MyBatis</a></li>
          <li><a href="../06.html">第6章 - MyBatis的核心配置</a></li>
          <li><a href="../07.html">第7章 - MyBatis的高级应用</a></li>
          <li><a href="../08.html">第8章 - Spring MVC入门</a></li>
          <li><a href="../09.html">第9章 - Spring MVC数据绑定</a></li>
          <li><a href="../10.html">第10章 - Spring MVC拦截器</a></li>
          <li><a href="../11.html">第11章 - Spring Boot基础</a></li>
          <li><a href="../12.html">第12章 - Spring Boot的Web开发</a></li>
          <li><a href="../13.html">第13章 - Spring Boot的数据库连接</a></li>
          <li><a href="../14.html">第14章 - 框架整合</a></li>
        </ul>
        <h5>实验课</h5>
        <ul class="uk-nav uk-nav-default doc-nav">
          <li><a href="./code-01.html">01</a></li>
          <li><a href="./code-02.html">02</a></li>
          <li><a href="./code-03.html">03</a></li>
          <li><a href="./code-04.html">04</a></li>
          <li><a href="./code-05.html">05</a></li>
          <li><a href="./code-06.html">06</a></li>
          <li><a href="./code-07.html">07</a></li>
          <li><a href="./code-08.html">08</a></li>
          <li class="uk-active"><a href="./code-09.html">09</a></li>
          <li><a href="./code-10.html">10</a></li>
          <li><a href="./code-11.html">11</a></li>
          <li><a href="./code-12.html">12</a></li>
        </ul>
      </div>
    </div>

    <footer class="uk-section uk-text-center uk-text-muted">
      <div class="uk-container uk-container-small">
        <div class="uk-margin-medium"></div>
      </div>
    </footer>

    <script src="../js/awesomplete.js"></script>
    <script src="../js/custom.js"></script>
  </body>
</html>
